﻿using MyCrypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DX_sharq.Models
{
    public class SecurityForAttribute : ActionFilterAttribute
    {
        Crypto _crypto = new Crypto();
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool b = true;

            if (filterContext.HttpContext.Request.Cookies["_C"] != null)
            {
                HttpCookie cookie = filterContext.HttpContext.Request.Cookies["_C"];
                string _guid = cookie.Value; // CookieClass dagi... даги guid билан бир хил булади
                if (HttpContext.Current.Session[_guid + "_app_ki"] != null)
                {
                    int authen = Convert.ToInt32(HttpContext.Current.Session[_guid + "_app_ki"]);
                    if (authen == 1)
                    {
                        int bulim_type = Convert.ToInt32(HttpContext.Current.Session[_guid + "_bul_t"]);
                        if (bulim_type == 2)
                        {
                            filterContext.HttpContext.Response.Redirect("/Admin/Admin_Index");
                        }
                        else
                            if (bulim_type == 4 || bulim_type == 3 || bulim_type == 1)
                            {
                                filterContext.HttpContext.Response.Redirect("/Section/Index");
                            }
                        b = true;
                    }
                }
                else
                {
                    b = false;
                }
            }
            else
            {
                b = false;
            }

            if (!b)
            {
                string url = filterContext.HttpContext.Request.Url.ToString();
                string ss = filterContext.RequestContext.HttpContext.Request.QueryString["mess"];
                filterContext.HttpContext.Response.Redirect("/account/LogOn/?mess=" + ss + "&returnUrl=" + url);
            }
        }
    }
}
